<?php

namespace CloudTowerDev\YvsYServer;

use YunTaIDC\Plugin\PluginBase;

use YunTaIDC\Events\CreateServiceEvent;
use YunTaIDC\Events\RenewServiceEvent;
use YunTaIDC\Events\DeleteserviceEvent;
use YunTaIDC\Events\LoginServiceEvent;
use YunTaIDC\Events\ProductConfigEvent;

class YvsYServer extends PluginBase{
    
    public function onLoad(){
        set_time_limit(0);
    }

    public function SendData(Array $array, String $url){
        // exit($url. http_build_query($array));
        $result = file_get_contents($url. http_build_query($array));
        $return = iconv('gb2312', 'UTF-8', $result);
        $return = str_replace(array(" ","　","\t","\n","\r"), "", $return);
        return json_decode($return, true);
    }
    
    public function CreateService(CreateServiceEvent $event){
        $Product = $event->getProduct();
        $Server = $Product->getServer();
        $Service = $event->getService();
        $Period = $Service->getPeriod();
        $Configoption = $Product->getConfigOption();
        $checktime = date('YmdHis').mt_rand(100,999);
        $params = array(
            'client' => $Server->getServerUsername(),
            'checktime' => $checktime,
            'format' => 'json',
            'method' => 'create',
            'classid' => 5,
            'checksum' => md5($Server->getServerUsername().md5($Server->getServerPassword()).'create'.$checktime),
            'Ftpname' => $Service->getUsername(),
            'period' => $Period['remark'],
            'ProductCode' => $Configoption['productcode'],
        );
        $url = $Server->getServerDomain().'api/api.asp?';
        $result = $this->SendData($params, $url);
        if($result['status']['code'] != '1'){
            $this->getSystem()->getLogger()->addSystemLog('创建服务'.$Service->getUsername().'失败，返回：'.json_decode($result));
            return false;
        }else{
            $array = array(
                'username' => $result['info']['hostname'],
                'password' => base64_encode($result['info']['password']),
            );
            $Service->set($array);
            $Service->setConfigOption(array('orderid'=>$result['info']['orderid']));
            return true;
        }
    }
    
    public function RenewService(RenewServiceEvent $event){
        $Product = $event->getProduct();
        $Server = $Product->getServer();
        $Service = $event->getService();
        $Period = $event->getPeriod();
        $Configoption = $Service->getConfigOption();
        if(empty($Configoption['orderid'])){
            $this->getSystem()->getLogger()->addSystemLog('续费服务'.$Service->getUsername().'失败，服务configoption为空');
            return false;
        }else{
            $checktime = date('YmdHis').mt_rand(100,999);
            $params = array(
                'client' => $Server->getServerUsername(),
                'checktime' => $checktime,
                'format' => 'json',
                'method' => 'renew',
                'classid' => 5,
                'checksum' => md5($Server->getServerUsername().md5($Server->getServerPassword()).'renew'.$checktime),
                'period' => $Period['remark'],
                'orderid' => $Configoption['orderid'],
            );
        }
        $url = $Server->getServerDomain().'api/api.asp?';
        $result = $this->SendData($params, $url);
        if($result['status']['code'] != '1'){
            $this->getSystem()->getLogger()->addSystemLog('续费服务'.$Service->getUsername().'失败，原因：'.$result['status']['message']);
            return false;
        }else{
            return true;
        }
    }
    
    public function DeleteService(DeleteServiceEvent $event){
        return true;
    }
    
    public function LoginService(LoginServiceEvent $event){
        $url = $event->getServer()->getServerDomain().'api/api.asp?';
        $checktime = date('YmdHis').mt_rand(100,999);
        $checksum = md5($event->getServer()->getServerUsername().md5($event->getServer()->getServerPassword())."panel".$checktime);
        echo '<form action="'.$url.'" method="GET"><input type="hidden" name="client" value="'.$event->getServer()->getServerUsername().'"><input type="hidden" name="method" value="panel"><input type="hidden" name="Ftpname" value="'.$event->getService()->getUsername().'"><input type="hidden" name="checktime" value="'.$checktime.'"><input type="hidden" name="checksum" value="'.$checksum.'"><input type="hidden" name="classid" value="5"><button type="submit">点击前往</button></form>';
    }
    
    public function ProductConfig(ProductConfigEvent $event){
        $Server = $event->getServer();
        $checktime = date('YmdHis').mt_rand(100,999);
        $params = array(
            'client' => $Server->getServerUsername(),
            'checktime' => $checktime,
            'format' => 'json',
            'method' => 'getproduct',
            'classid' => 5,
            'checksum' => md5($Server->getServerUsername().md5($Server->getServerPassword()).'getproduct'.$checktime),
        );
        $url = $Server->getServerDomain().'api/api.asp?';
        $result = $this->SendData($params, $url);
        if($result['status']['code'] == 1){
            foreach($result['info']['item'] as $k => $v){
                $option[$v['classname']." : ".$v['ProductName']] = $v['ProductCode'];
            }
        }else{
            $option[$result['status']['message']] = '0';
        }
        return array(
            'productcode' => array('label' => '对接产品', 'type' => "select", 'option' => $option),
        );
    }
    
}

?>